﻿using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace XExcel
{
    internal static class Common
    {
        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>
        public static string ToSbc(this string input)
        {
            if(string.IsNullOrWhiteSpace(input))
                return input;
            // 半角转全角：
            char[] array = input.ToCharArray();
            for(int i = 0; i < array.Length; i++)
            {
                if(array[i] == 32)
                {
                    array[i] = (char)12288;
                    continue;
                }
                if(array[i] < 127)
                {
                    array[i] = (char)(array[i] + 65248);
                }
            }
            return new string(array);
        }

        /// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>
        public static string ToDbc(this string input)
        {
            if(string.IsNullOrWhiteSpace(input))
                return input;
            char[] array = input.ToCharArray();
            for(int i = 0; i < array.Length; i++)
            {
                if(array[i] == 12288)
                {
                    array[i] = (char)32;
                    continue;
                }
                if(array[i] > 65280 && array[i] < 65375)
                {
                    array[i] = (char)(array[i] - 65248);
                }
            }
            return new string(array);
        }

        private static readonly Regex space = new Regex(@"\s+");
        /// <summary>
        /// 将字串中多个连续空格替换成一个空格
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveMoreSpace(this string input)
        {
            if(input == null)
                return null;

            return space.Replace(input, " ");
        }
        /// <summary>
        /// 判断一个类型是匿名类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsAnonymousType(this Type type)
        {
            if(type == null)
            {
                return false;
            }
            Type d = type;
            if(d.Name.StartsWith("<>f__AnonymousType"))
            {
                return true;
            }
            if(type.IsGenericType)
            {
                d = type.GetGenericTypeDefinition();
            }
            if(d.Name.StartsWith("<>f__AnonymousType"))
            {
                return true;
            }

            if(d.IsClass && d.IsSealed && d.IsNotPublic)
            {
                var attributes = d.GetCustomAttribute<CompilerGeneratedAttribute>(false);
                if(attributes != null)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断一个类型是Nullable
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsNullableType(this Type type)
        {
            return type != null && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
        }
        /// <summary>
        /// 返回一个类型的不可空类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type GetNonNullableType(this Type type)
        {
            if(IsNullableType(type))
                return type.GetGenericArguments()[0];

            return type;
        }
        public static object GetDefaultValue(this Type type)
        {
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
        public static object GetNullOrError(this Type type, string message = "值不能为空")
        {
            if(!type.IsValueType || type.IsNullableType())
            {
                return null;
            }
            throw new Exception(message);
        }
    }
}